/**
 * Copyright (c) 2010 Simple Graph Tools contributors. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * $Id$
 */
package org.sgt.coredef.impl;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;
import org.sgt.coredef.CoredefPackage;
import org.sgt.coredef.Description;
import org.sgt.coredef.Proxy;
import org.sgt.coredef.util.ProxyHelper;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Proxy</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link org.sgt.coredef.impl.ProxyImpl#getName <em>Name</em>}</li>
 *   <li>{@link org.sgt.coredef.impl.ProxyImpl#getSummary <em>Summary</em>}</li>
 *   <li>{@link org.sgt.coredef.impl.ProxyImpl#getID <em>ID</em>}</li>
 *   <li>{@link org.sgt.coredef.impl.ProxyImpl#getDescription <em>Description</em>}</li>
 *   <li>{@link org.sgt.coredef.impl.ProxyImpl#getRepresentedObject <em>Represented Object</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class ProxyImpl<T> extends EObjectImpl implements Proxy<T> {
	/**
	 * The default value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected static final String NAME_EDEFAULT = "<name>";

	/**
	 * The cached value of the '{@link #getName() <em>Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getName()
	 * @generated
	 * @ordered
	 */
	protected String name = NAME_EDEFAULT;

	/**
	 * The default value of the '{@link #getSummary() <em>Summary</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getSummary()
	 * @generated
	 * @ordered
	 */
	protected static final String SUMMARY_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getSummary() <em>Summary</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getSummary()
	 * @generated
	 * @ordered
	 */
	protected String summary = SUMMARY_EDEFAULT;

	/**
	 * The default value of the '{@link #getID() <em>ID</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getID()
	 * @generated
	 * @ordered
	 */
	protected static final String ID_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getID() <em>ID</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getID()
	 * @generated
	 * @ordered
	 */
	protected String id = ID_EDEFAULT;

	/**
	 * The cached value of the '{@link #getDescription() <em>Description</em>}' containment reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDescription()
	 * @generated
	 * @ordered
	 */
	protected Description description;

	/**
	 * The cached value of the '{@link #getRepresentedObject() <em>Represented Object</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getRepresentedObject()
	 * @generated
	 * @ordered
	 */
	protected T representedObject;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected ProxyImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return CoredefPackage.Literals.PROXY;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getName() {
		return name;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setName(String newName) {
		String oldName = name;
		name = newName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, CoredefPackage.PROXY__NAME, oldName, name));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getSummary() {
		return summary;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setSummary(String newSummary) {
		String oldSummary = summary;
		summary = newSummary;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, CoredefPackage.PROXY__SUMMARY, oldSummary, summary));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getID() {
		return id;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setID(String newID) {
		String oldID = id;
		id = newID;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, CoredefPackage.PROXY__ID, oldID, id));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Description getDescription() {
		return description;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetDescription(Description newDescription,
			NotificationChain msgs) {
		Description oldDescription = description;
		description = newDescription;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, CoredefPackage.PROXY__DESCRIPTION, oldDescription, newDescription);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setDescription(Description newDescription) {
		if (newDescription != description) {
			NotificationChain msgs = null;
			if (description != null)
				msgs = ((InternalEObject)description).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - CoredefPackage.PROXY__DESCRIPTION, null, msgs);
			if (newDescription != null)
				msgs = ((InternalEObject)newDescription).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - CoredefPackage.PROXY__DESCRIPTION, null, msgs);
			msgs = basicSetDescription(newDescription, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, CoredefPackage.PROXY__DESCRIPTION, newDescription, newDescription));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	public T getRepresentedObject() {
		if (representedObject != null && ((EObject)representedObject).eIsProxy()) {
			InternalEObject oldRepresentedObject = (InternalEObject)representedObject;
			representedObject = (T)eResolveProxy(oldRepresentedObject);
			if (representedObject != oldRepresentedObject) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, CoredefPackage.PROXY__REPRESENTED_OBJECT, oldRepresentedObject, representedObject));
			}
		}
		return representedObject;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public T basicGetRepresentedObject() {
		return representedObject;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setRepresentedObject(T newRepresentedObject) {
		T oldRepresentedObject = representedObject;
		representedObject = newRepresentedObject;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, CoredefPackage.PROXY__REPRESENTED_OBJECT, oldRepresentedObject, representedObject));
	}

	/**
	 * <!-- begin-user-doc -->
	 * Determines whether the {@link Proxy} is in sync with its
	 * representedObject.
	 * @return true only if the synchronization was complete and successful.
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public boolean isSynchronised(EList<EStructuralFeature> featuresList) {
		return ProxyHelper.isSynchronised(this, featuresList);
	}

	/**
	 * <!-- begin-user-doc -->
	 * Synchronizes the representation values from the ones
	 * in the proxy. This default implementation does nothing
	 * and returns false.
	 * @param proxy the {@link Proxy} to synchronize values from.
	 * @param featuresList the {@link Proxy} features list to be
	 * the sources of synchronization of the representation.
	 * @return true only if synchronization was complete and successful.
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public boolean syncRepresentation(EList<EStructuralFeature> featuresList) {
		return ProxyHelper.syncRepresentation(this, featuresList);
	}

	/**
	 * <!-- begin-user-doc -->
	 * Synchronizes the {@link Proxy} values from the ones
	 * in the representation.
	 * @param proxy the {@link Proxy} to be synchronized.
	 * @param featuresList the {@link EObject} represented Object's
	 * features list to be the sources of the {@link Proxy}
	 * synchronization.
	 * @return true if synchronization was complete and successful.
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public boolean syncProxy(EList<EStructuralFeature> featuresList) {
		return ProxyHelper.syncProxy(this, featuresList);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd,
			int featureID, NotificationChain msgs) {
		switch (featureID) {
			case CoredefPackage.PROXY__DESCRIPTION:
				return basicSetDescription(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case CoredefPackage.PROXY__NAME:
				return getName();
			case CoredefPackage.PROXY__SUMMARY:
				return getSummary();
			case CoredefPackage.PROXY__ID:
				return getID();
			case CoredefPackage.PROXY__DESCRIPTION:
				return getDescription();
			case CoredefPackage.PROXY__REPRESENTED_OBJECT:
				if (resolve) return getRepresentedObject();
				return basicGetRepresentedObject();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case CoredefPackage.PROXY__NAME:
				setName((String)newValue);
				return;
			case CoredefPackage.PROXY__SUMMARY:
				setSummary((String)newValue);
				return;
			case CoredefPackage.PROXY__ID:
				setID((String)newValue);
				return;
			case CoredefPackage.PROXY__DESCRIPTION:
				setDescription((Description)newValue);
				return;
			case CoredefPackage.PROXY__REPRESENTED_OBJECT:
				setRepresentedObject((T)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case CoredefPackage.PROXY__NAME:
				setName(NAME_EDEFAULT);
				return;
			case CoredefPackage.PROXY__SUMMARY:
				setSummary(SUMMARY_EDEFAULT);
				return;
			case CoredefPackage.PROXY__ID:
				setID(ID_EDEFAULT);
				return;
			case CoredefPackage.PROXY__DESCRIPTION:
				setDescription((Description)null);
				return;
			case CoredefPackage.PROXY__REPRESENTED_OBJECT:
				setRepresentedObject((T)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case CoredefPackage.PROXY__NAME:
				return NAME_EDEFAULT == null ? name != null : !NAME_EDEFAULT.equals(name);
			case CoredefPackage.PROXY__SUMMARY:
				return SUMMARY_EDEFAULT == null ? summary != null : !SUMMARY_EDEFAULT.equals(summary);
			case CoredefPackage.PROXY__ID:
				return ID_EDEFAULT == null ? id != null : !ID_EDEFAULT.equals(id);
			case CoredefPackage.PROXY__DESCRIPTION:
				return description != null;
			case CoredefPackage.PROXY__REPRESENTED_OBJECT:
				return representedObject != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (name: ");
		result.append(name);
		result.append(", summary: ");
		result.append(summary);
		result.append(", ID: ");
		result.append(id);
		result.append(')');
		return result.toString();
	}

} //ProxyImpl
